﻿
// ProjectAADlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "ProjectAA.h"
#include "ProjectAADlg.h"
#include "afxdialogex.h"
#include "LoginDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//ImgCap *imgCap;
//ImgFile imgFile; 
//CTS CTSCam;

// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CProjectAADlg 对话框



CProjectAADlg::CProjectAADlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_PROJECTAA_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CProjectAADlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CProjectAADlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_32771, &CProjectAADlg::OnOpenLogDlg)
	ON_COMMAND(ID_32773, &CProjectAADlg::OnOpenMotorDlg)
	ON_COMMAND(ID_32772, &CProjectAADlg::OnOpenProjectDlg)
	ON_BN_CLICKED(IDC_BUTTON_START, &CProjectAADlg::OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, &CProjectAADlg::OnBnClickedButtonStop)
	ON_COMMAND(ID_32776, &CProjectAADlg::OnOpenCaptureDlg)
	ON_COMMAND(ID_32777, &CProjectAADlg::OnOpenSFRDlg)
	ON_WM_SIZE()
	ON_COMMAND(ID_32774, &CProjectAADlg::OnOpenTcpipDlg)
	ON_MESSAGE(WM_SEND_SOCKET_SERVER_MSG_TO_MAIN_UI, &CProjectAADlg::OnReceiveServerMsg)
	ON_COMMAND(ID_32778, &CProjectAADlg::OnOpwnWhiteDlg)
	ON_BN_CLICKED(IDC_BUTTON_SAVE, &CProjectAADlg::OnBnClickedButtonSave)
	ON_COMMAND(ID_LOGIN, &CProjectAADlg::OnLogin)
END_MESSAGE_MAP()


// CProjectAADlg 消息处理程序

BOOL CProjectAADlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	gSystemMgr.GeneralSettingIniInit();//读取型号配置

	m_menu.LoadMenuW(IDR_MENU1);
	this->SetMenu(&m_menu);

	gSystemMgr.CaptureConfigIniInit();//采图配置
	gSystemMgr.TcpipConfigIniInit();//Tcpip通讯配置

	gSystemMgr.m_ptrMainUI = this;

	if (m_cLogDlg == nullptr)
	{
		m_cLogDlg = new CLogDlg;
		m_cLogDlg->Create(IDD_DIALOG_LOG, this);
	}
	m_cLogDlg->ShowWindow(SW_HIDE);


	//bool bInit = gSystemMgr.MN200Init();//轴卡初始化
	//if (bInit)
	//{
	//	gSystemMgr.MotorInit();//轴配置
	//	gSystemMgr.StartGetMotorIoThread();//状态？
	//	gSystemMgr.StartGetMotorEncoderThread();//位置？
	//}

	switch (gSystemMgr.m_nImageSource) {
	case IMG_DEV_FILE:
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgFile;
		//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"Image Files");
		break;
	case IMG_DEV_CTS:
		Cam_Init();
		gSystemMgr.m_bCTSInitFlag = 1;
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgCTS;
		//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"CTS Cam");
		break;
	case IMG_RO_LONGO:
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgRLG;
		//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"RLG Cam");
		//gSystemMgr.imgRLG.init_start();
		break;
	}
	gSystemMgr.StartCaptureThread();
	gSystemMgr.StartImageTestThread();
	gSystemMgr.LoginConfigIniInit();

	gSystemMgr.imgCap->CapMutex = ::CreateEventW(NULL, TRUE, FALSE, L"CapTest");
	SetEvent(gSystemMgr.imgCap->CapMutex);

	CWnd *pWnd;
	pWnd = GetDlgItem(IDC_DISPLAY);
	pWnd->GetWindowRect(&OriRect[0]);
	RectW[0] = OriRect[0].right - OriRect[0].left;
	RectH[0] = OriRect[0].bottom - OriRect[0].top;

	//pWnd = GetDlgItem(IDC_SFR_CURVE);
	pWnd = GetDlgItem(IDC_STATIC_RESULT);
	pWnd->GetWindowRect(&OriRect[1]);
	RectW[1] = OriRect[1].right - OriRect[1].left;
	RectH[1] = OriRect[1].bottom - OriRect[1].top;

	pWnd = GetDlgItem(IDC_BUTTON_START);
	pWnd->GetWindowRect(&OriRect[2]);
	RectW[2] = OriRect[2].right - OriRect[2].left;
	RectH[2] = OriRect[2].bottom - OriRect[2].top;

	pWnd = GetDlgItem(IDC_BUTTON_STOP);
	pWnd->GetWindowRect(&OriRect[3]);
	RectW[3] = OriRect[3].right - OriRect[3].left;
	RectH[3] = OriRect[3].bottom - OriRect[3].top;

	pWnd = GetDlgItem(IDC_BUTTON_SAVE);
	pWnd->GetWindowRect(&OriRect[4]);
	RectW[4] = OriRect[4].right - OriRect[4].left;
	RectH[4] = OriRect[4].bottom - OriRect[4].top;

	GetClientRect(&m_rcUiRect);

	if (!gSystemMgr.changeImg.init_lib()) {
		//logdlg.log("convert init error");
		gSystemLog.DisplayLogAndSave(L"convert init error", LV_ERROR);
	}

	//Tcpip通信自动建立服务器 或 自动连接
	if (gSystemMgr.m_nModeSelect == 0 && gSystemMgr.m_bAutoFlag) {
		USES_CONVERSION;
		char* c_strIp = T2A(gSystemMgr.m_cstrIPAdd);
		bool bCreateServer = gSystemMgr.m_cSocketServer.OpenSocketServer("Server", c_strIp, gSystemMgr.m_nPort);
		CString cstr;
		cstr.Format(L"Socket Server Build result:%d", bCreateServer);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_ERROR); 
		if (bCreateServer)
		{
			gSystemMgr.StartSocketAcceptClientThread();
		}
	}
	gSystemMgr.StartTestThread();

	SetWindowText(L"白场测试-v1.2.3 [权限管控+白场亮度测试]");

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CProjectAADlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CProjectAADlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CProjectAADlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CProjectAADlg::OnOpenLogDlg()
{
	if (m_cLogDlg != nullptr)
	{
		m_cLogDlg->ShowWindow(SW_SHOW);
	}
	//else
	//{
	//	m_cLogDlg = new CLogDlg;
	//	m_cLogDlg->Create(IDD_DIALOG_LOG, this);
	//}
}


void CProjectAADlg::OnOpenMotorDlg()
{
	if (m_cMotorDlg != nullptr)
	{
		delete m_cMotorDlg;
		m_cMotorDlg = nullptr;
	}
	if (m_cMotorDlg == nullptr)
	{
		m_cMotorDlg = new CMotorDlg;
		m_cMotorDlg->Create(IDD_DIALOG_MOTOR, this);
	}
	m_cMotorDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenProjectDlg()
{
	if (m_cChangeProjectDlg != nullptr)
	{
		delete m_cChangeProjectDlg;
		m_cChangeProjectDlg = nullptr;
	}
	if (m_cChangeProjectDlg == nullptr)
	{
		m_cChangeProjectDlg = new CChangeProject;
		m_cChangeProjectDlg->Create(IDD_DIALOG_CHANGE_PROJECT, this);
	}
	m_cChangeProjectDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedButtonStart()
{
	gSystemMgr.m_bStopCapture = 0;
	CWnd* pBtn = nullptr;
	pBtn = (CWnd*)GetDlgItem(IDC_BUTTON_START);
	pBtn->EnableWindow(FALSE);
}


void CProjectAADlg::OnBnClickedButtonStop()
{
	gSystemMgr.m_bStopCapture = 1;
	gSystemMgr.imgCap->m_bInitFlag = FALSE;
	CWnd* pBtn = nullptr;
	pBtn = (CWnd*)GetDlgItem(IDC_BUTTON_START);
	pBtn->EnableWindow(TRUE);
}


void CProjectAADlg::OnOpenCaptureDlg()
{
	if (m_cCaptureDlg != nullptr)
	{
		delete m_cCaptureDlg;
		m_cCaptureDlg = nullptr;
	}
	if (m_cCaptureDlg == nullptr)
	{
		m_cCaptureDlg = new CImageCaptureDlg;
		m_cCaptureDlg->Create(IDD_DIALOG_CAPTURE, this);
	}
	m_cCaptureDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnOpenSFRDlg()
{
	if (m_cSfrSettingDlg != nullptr)
	{
		delete m_cSfrSettingDlg;
		m_cSfrSettingDlg = nullptr;
	}
	if (m_cSfrSettingDlg == nullptr)
	{
		m_cSfrSettingDlg = new CSFRSettingDlg;
		m_cSfrSettingDlg->Create(IDD_DIALOG_SFR, this);
	}
	m_cSfrSettingDlg->ShowWindow(SW_SHOW);
}

void CProjectAADlg::Repaint(CWnd * pWnd, int last_Width, int now_Width, int last_Height, int now_Height, int cnt)
{
	if (pWnd)
	{
		CRect rect;
		CRect rect2;
		pWnd->GetWindowRect(&rect2);
		rect = OriRect[cnt];
		int n = 60;
		//rect2.top = rect2.top - n;
		//rect2.bottom = rect2.bottom - n;
		ScreenToClient(&rect2);

		double proportion_x = now_Width / (double)last_Width;
		double proportion_y = now_Height / (double)last_Height;

		rect.left = (long)(rect.left*proportion_x + 0.5);
		rect.right = (long)(rect.left+ RectW[cnt] *proportion_x + 0.5);
		rect.top = (long)(rect.top *proportion_y + 0.5 - n * proportion_y);
		rect.bottom = (long)(rect.top+ RectH[cnt] *proportion_y + 0.5);
		pWnd->MoveWindow(&rect);
	}

}

void CProjectAADlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	if (0 == m_rcUiRect.left && 0 == m_rcUiRect.right && 0 == m_rcUiRect.top && 0 == m_rcUiRect.bottom)//第一次启动对话框时的大小变化不做处理
	{
	}
	else
	{
		if (0 == cx && 0 == cy)//如果是按下了最小化，则触发条件，这时不保存对话框数据
		{
			return;
		}
		CRect rectDlgChangeSize;
		GetClientRect(&rectDlgChangeSize);//存储对话框大小改变后对话框大小数据

		//if (rectDlgChangeSize.Width() > 400 && rectDlgChangeSize.Height() > 200) {
		CWnd *pWnd;
		pWnd = GetDlgItem(IDC_DISPLAY);
		Repaint(pWnd, m_rcUiRect.Width(), rectDlgChangeSize.Width(), m_rcUiRect.Height(), rectDlgChangeSize.Height(), 0);
		
		//pWnd = GetDlgItem(IDC_SFR_CURVE);
		pWnd = GetDlgItem(IDC_STATIC_RESULT);
		Repaint(pWnd, m_rcUiRect.Width(), rectDlgChangeSize.Width(), m_rcUiRect.Height(), rectDlgChangeSize.Height(), 1);

		pWnd = GetDlgItem(IDC_BUTTON_START);
		Repaint(pWnd, m_rcUiRect.Width(), rectDlgChangeSize.Width(), m_rcUiRect.Height(), rectDlgChangeSize.Height(), 2);

		pWnd = GetDlgItem(IDC_BUTTON_STOP);
		Repaint(pWnd, m_rcUiRect.Width(), rectDlgChangeSize.Width(), m_rcUiRect.Height(), rectDlgChangeSize.Height(), 3);

		pWnd = GetDlgItem(IDC_BUTTON_SAVE);
		Repaint(pWnd, m_rcUiRect.Width(), rectDlgChangeSize.Width(), m_rcUiRect.Height(), rectDlgChangeSize.Height(), 4);
		//}
	}
	//GetClientRect(&m_rcUiRect); //save size of dialog
	Invalidate();//更新窗口
}


void CProjectAADlg::OnOpenTcpipDlg()
{
	if (m_cTcpipDlg != nullptr)
	{
		delete m_cTcpipDlg;
		m_cTcpipDlg = nullptr;
	}
	if (m_cTcpipDlg == nullptr)
	{
		m_cTcpipDlg = new CTcpipDlg;
		m_cTcpipDlg->Create(IDD_DIALOG_TCPIP, this);
	}
	m_cTcpipDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnCancel()
{
	gSystemMgr.m_cSocketServer.CloseSocketServer();

	gSystemMgr.m_bStopCapture = 1;
	gSystemMgr.imgCap->m_bInitFlag = FALSE;
	gSystemMgr.imgFile.release();
	gSystemMgr.imgCTS.release();
	gSystemMgr.imgRLG.release();

	CDialogEx::OnCancel();
}

LRESULT CProjectAADlg::OnReceiveServerMsg(WPARAM wParam, LPARAM lParam)
{
	CString cstrp = (BSTR)wParam;
	SysFreeString((BSTR)wParam);

	gSystemLog.DisplayLogAndSave(cstrp.GetBuffer(), LV_EVENT);

	std::vector<wstring> vResultItem = _utils::SplitString(cstrp.GetBuffer(), L";");

	if (vResultItem.size() == 2)
	{
		if (vResultItem[0] == L"Project")
		{
			CString cstr(L"新更换的型号为");
			cstr.Append(_utils::Unicode2TChar(vResultItem[1]));
			gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
			gSystemMgr.m_cGeneralSetting.SaveConfigIni(L"GeneralSetting", L"m_cstrProductName", _utils::Unicode2TChar(vResultItem[1]));

			gSystemMgr.GeneralSettingIniInit();
			gSystemMgr.CaptureConfigIniInit();//采图配置

			switch (gSystemMgr.m_nImageSource) {
			case IMG_DEV_FILE:
				gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgFile;
				//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"Image Files");
				break;
			case IMG_DEV_CTS:
				Cam_Init();
				gSystemMgr.m_bCTSInitFlag = 1;
				gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgCTS;
				//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"CTS Cam");
				break;
			case IMG_RO_LONGO:
				gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgRLG;
				//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"RLG Cam");
				//gSystemMgr.imgRLG.init_start();
				break;
			}
		}
	}

	//WHITETEST;
	if (vResultItem[0] == L"WHITETEST")
	{
		if (vResultItem.size() == 2)
		{
			gSystemMgr.SaveSN.Format(_utils::Unicode2TChar(vResultItem[1]));
		}
		if (vResultItem.size() == 3)
		{
			gSystemMgr.SaveSN.Format(_utils::Unicode2TChar(vResultItem[1]));
			cstrp.Format(_utils::Unicode2TChar(vResultItem[2]));
			gSystemMgr.PallentID = _ttoi(cstrp);
		}
		gSystemLog.DisplayLogAndSave(L"接收到开始白场测试信号", LV_EVENT);
		gSystemMgr.StainNum = 0;
		gSystemMgr.m_nStartWhiteTest = 1;
	}

	if (vResultItem[0] == L"WHITECAP")
	{
		gSystemLog.DisplayLogAndSave(L"接收到开始白场出图信号", LV_EVENT);
		gSystemMgr.m_nStartWhiteCap = 1;
	}

	return LRESULT();
}

void CProjectAADlg::OnOpwnWhiteDlg()
{
	if (m_cWhiteDlg != nullptr)
	{
		delete m_cWhiteDlg;
		m_cWhiteDlg = nullptr;
	}
	if (m_cWhiteDlg == nullptr)
	{
		m_cWhiteDlg = new CWhiteTestDlg;
		m_cWhiteDlg->Create(IDD_DIALOG_WHITE, this);
	}
	m_cWhiteDlg->ShowWindow(SW_SHOW);
}


void CProjectAADlg::OnBnClickedButtonSave()
{
	gSystemMgr.SaveTestReport_White(gSystemMgr.imgCap->bBmpTestBuf, gSystemMgr.imgCap->iBmpW, gSystemMgr.imgCap->iBmpH);
}

void CProjectAADlg::OnLogin()
{
	CLoginDlg loginDlg;
	loginDlg.DoModal();
}
